<template>
  <div>
    <!-- 搜索框 -->
    <el-card id="search">
      <el-row>
        <el-col align="right">
          <el-button
            @click="openEditUI(null)"
            type="success"
            icon="el-icon-plus"
            circle
          ></el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 结果表格 -->
    <el-card>
      <el-table :data="dataList" stripe style="width: 100%">
        <el-table-column label="#" width="80">
          <template slot-scope="scope">
            <!-- (pageNo - 1) * pageSize + index + 1 -->
            {{
              (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="id" label="公告ID" width="180" align="center">
        </el-table-column>
        <el-table-column prop="content" label="内容" width="480" align="center">
        </el-table-column>
        <el-table-column prop="publishTime" label="上传时间" width="180" align="center">
        </el-table-column>
        <el-table-column label="操作" width="220" align="center">
          <template slot-scope="scope">
            <el-button
              @click="openEditUI(scope.row)"
              type="primary"
              icon="el-icon-edit"
              circle
            ></el-button>
            <el-button
              @click="deleteRow(scope.row)"
              type="danger"
              icon="el-icon-delete"
              circle
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 公告信息编辑对话框 -->
    <el-dialog
      @close="clearForm"
      :title="title"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="form" ref="formRef" :rules="rules">
        <el-form-item label="公告内容" prop="content">
          <el-input type="textarea" :rows="4" placeholder="请输入内容" v-model="form.content">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import noticeApi from "@/api/noticeManage";
export default {
  data() {
    return {
      editorOption:{},
      imageUrl: '',
      uploadUrl: 'http://localhost:8080/common/upload',
      downloadImageUrl: 'http://localhost:8080/common/download?name=',
      roleList: [],
      formLabelWidth: "130px",
      form: {},
      dialogFormVisible: false,
      title: "",
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      dataList: [],
      rules: {
        content: [
          { required: true, message: "请输入公告内容", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    deleteRow(row) {
      this.$confirm(`确定删除公告 ${row.id} ?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          noticeApi.deleteById(row.id).then((response) => {
            this.$message({
              type: "success",
              message: response.message,
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    save() {
      //触发表单验证
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          //提交请求给后台
          noticeApi.save(this.form).then((response) => {
            if (response.code == '200'){
              this.$message.success(response.message)
            }else{
              this.$message.error(response.message)
            }
            // 关闭对话框
            this.dialogFormVisible = false;
            // 刷新表格
            this.getList();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    clearForm() {
      this.form = {};
      this.$refs.formRef.clearValidate();
    },
    openEditUI(row) {
      if (row == null) {
        this.title = "新增公告";
      } else {
        this.title = "修改公告";
        this.form = row;
      }
      this.dialogFormVisible = true;
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize;
      this.getList();
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo;
      this.getList();
    },
    beforeImageUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 格式或者是PNG!')
      }

      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    handleImageSuccess(response, file) {
      this.imageUrl =
        'http://localhost:8080/common/download?name=' + response.data
      this.form.coverImage = response.data
      console.log('上传的封面:',this.form.coverImage)
    },
    getList() {
      noticeApi.getList(this.searchModel).then((response) => {
        this.dataList = response.data.rows;
        this.total = response.data.total;
      });
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}
.el-dialog .el-input {
  width: 35%;
}
</style>
